<style>
.pure_table_extra { width: 750px; margin-top: 5px; }
.pure_table_extra img { cursor: pointer; margin-right: 2px; }
.pure_table_extra th { height: 25px; line-height: 25px; cursor: pointer; }
.pure_table_extra td { vertical-align: middle; }
.smaller span { font-size: 11px !important; padding: 0.2em 1em !important; }
</style>

<?php
$pg = Utils::get_param('pg', 1);
$q = Utils::get_param('q', 'ASC');
$o = Utils::get_param('o', 't.account');
$search = json_decode(Utils::get_param('search', '[]'));

$opts = new stdClass();
if (isset($search->type) && isset($search->phrase)) {
  if ($search->type == 1) {
    $opts->cd = 't.status = :v1';
    $opts->cdv = array(':v1' => 0);
  } else if ($search->type == 2) {
    $opts->cd = 't.status = :v1';
    $opts->cdv = array(':v1' => 1);
  } else if ($search->type == 3) {
    $opts->cd = 't.status = :v1';
    $opts->cdv = array(':v1' => 2);
  }
  
  if ($search->type != 0) {
    $opts->cd .= ' AND ';
  }
  
  $opts->cd .= '(t.account LIKE :v3 OR t.first_name LIKE :v3 OR t.last_name LIKE :v3)';
  $opts->cdv[":v3"] = "%$search->phrase%";
}
$data = $orm->get('pb_ht_customer', $o, $q, $pg, SIZE, $opts);

?>

<div class="pure-form">
  <input id="find_name" type="text" style="width: 250px;" placeholder="{Search}" onkeypress="if(event.which == 13) find();">
  <select id="find_type">
    <option value="0">All</option>
    <option value="1">Inactivated</option>
    <option value="2">Activated</option>
    <option value="3">Suspended</option>
  </select>
  <button type="submit" class="pure-button pure-button-primary" onclick="find();">Go</button>
</div>
<table class="pure-table pure_table_extra ui-corner-all" cellpadding=0 cellspacing=0>
  <thead>
    <tr>
      <th class="ui-widget-header" onclick="return set_order('account');">邮箱</th>
      <th class="ui-widget-header" style="width: 80px;" onclick="return set_order('last_name');">姓</th>
      <th class="ui-widget-header" style="width: 80px;" onclick="return set_order('first_name');">名</th>
      <th class="ui-widget-header" style="width: 120px;" onclick="return set_order('status');">状态</th>
      <th class="ui-widget-header" style="width: 80px;" onclick="return set_order('reg_date');">注册日期</th>
      <th class="ui-widget-header" style="width: 40px;">&nbsp;</th>
    </tr>
  </thead>
  <tbody id="data_body"></tbody>
</table>
<div>&nbsp;</div>
<div id="pagination"></div>

<script type="text/javascript">
  /*<![CDATA[*/
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
  var __requrl = '<?php echo Utils::get_requrl($app_data); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __search = JSON.parse('<?php echo addslashes(json_encode($search)); ?>');
  
  $(function(){
    render();
  });

  function render() {
    $('#pagination').pagination({
      items: __data.t,
      itemsOnPage: __data.s,
      currentPage: __data.p,
      cssStyle: 'compact-theme',
      onPageClick: function(pageNumber, event) {
        return Url.redirect(__redmd, __sys + '.php?i=' + __id + '&pg=' + pageNumber + '&o=' + __data.o + '&q=' + __data.q + '&search=' + Utils.encodeURI(JSON.stringify(__search)));
      }
    });
    render_data();
  };
  
  function render_data() {
    Url.insert_param('o', __data.o);
    Url.insert_param('q', __data.q);
    Url.insert_param('search', Utils.encodeURI(JSON.stringify(__search)));
    
    if (__search.type) {
      $('#find_type').val(__search.type);
    }
    if (__search.phrase) {
      $('#find_name').val(__search.phrase);
    }
    
    $('#data_body').empty();
    for (var idx in __data.d) {
      var itm = __data.d[idx];
      $('#data_body').append(
        '<tr class="' + ((idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + '">' + 
          '<td>' + itm.account + '</td>' +
          '<td title="' + itm.last_name + '">' + Utils.get_short(itm.last_name, 10) + '</td>' +
          '<td title="' + itm.first_name + '">' + Utils.get_short(itm.first_name, 10) + '</td>' +
          '<td>' +
            '<select style="width: 100%" id="status_' + itm.id + '" onchange="set_status(' + itm.id + ')">' +
              '<option value="0">Inactivated</option>' +
              '<option value="1">Activated</option>' +
              '<option value="2">Suspended</option>' +
            '</select>' +
          '</td>' +
          '<td>' + itm.reg_date + '</td>' +
          '<td>' +
            '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-pencil" onclick="Url.redirect(' + __redmd + ', \'' + __sys + '.php?i=' + __id + '&__file=index_customer&id=' + itm.id + '&pg=' + __data.p + '\');"></span>' +
          '</td>' + 
        '</tr>'
      );
      $('#status_' + itm.id).val(itm.status);
    }
    if (__data.d.length == 0) {
      $('#data_body').append('<tr class="pure-table-even"><td colspan=12>None</td></tr>');
    }
    $('#data_body input[type=checkbox]').button();
  };


  function __get_search() {
    return {
      type: $('#find_type option:selected').val(),
      phrase: $('#find_name').val()
    };
  };

  function set_status(id) {
    var req = Utils.get_by_id(id, __data.d);
    req.status = $('#status_' + id + ' option:selected').val();
    Ajax.request(
      __requrl + 'submit_obj',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
      },
      null
    );
  };
  
  function set_order(o) {
    var req = {
      q: (__data.q) == 'ASC' ? 'DESC' : 'ASC',
      o: o,
      pg: __data.p,
      search: __search
    };
    Ajax.request(
      __requrl + 'get_objs',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        __data = rslt;
        render();
      },
      null
    );
  };

  function find() {
    __search = __get_search();
    
    var req = {
      q: __data.q,
      o: __data.o,
      pg: __data.p,
      search: __search
    };
    Ajax.request(
      __requrl + 'get_objs',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        __data = rslt;
        render();
      },
      null
    );
  };
  /*]]>*/
</script>